package com.emuporium.gymrat.server.log;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.ExerciseDB;
import com.emuporium.gymrat.server.data.LogDB;
import com.emuporium.gymrat.server.data.LogEntry;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;
import com.emuporium.gymrat.server.data.Variation;
import com.emuporium.gymrat.server.data.Equipment;

@SuppressWarnings("serial")
public class LogShowEntryServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int logId = new Integer(request.getParameter("id"));
		User user = UserDB.selectUser(request.getRemoteUser());

		ArrayList<LogEntry> log = LogDB.selectLogEntries(user.getId(), logId);
		assert (log.size() == 1);

		ArrayList<Variation> variations = ExerciseDB.selectVariations(log
				.get(0).getExerciseId(), user.getId());
		ArrayList<Equipment> equipment = ExerciseDB.selectEquipment(log.get(0)
				.getExerciseId(), user.getId());
		
		request.setAttribute("variations", variations);
		request.setAttribute("equipment", equipment);

		request.setAttribute("log", log.get(0));
		String url = "/log/showLogEntry.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

}
